﻿CREATE PROCEDURE [dbo].[yaf_choice_vote](
                @ChoiceID INT,
                @UserID   INT  = NULL,
                @RemoteIP NVARCHAR(10)  = NULL)
AS
    BEGIN
        DECLARE  @PollID INT
        SET @PollID = (SELECT PollID
                       FROM   yaf_Choice
                       WHERE  ChoiceID = @ChoiceID)
        IF @UserID = NULL
        BEGIN
            IF @RemoteIP != NULL
            BEGIN
                INSERT INTO yaf_PollVote
                           (PollID,
                            UserID,
                            RemoteIP)
                VALUES     (@PollID,
                            NULL,
                            @RemoteIP)
            END
        END
        ELSE
        BEGIN
            INSERT INTO yaf_PollVote
                       (PollID,
                        UserID,
                        RemoteIP)
            VALUES     (@PollID,
                        @UserID,
                        @RemoteIP)
        END
        UPDATE yaf_Choice
        SET    Votes = Votes + 1
        WHERE  ChoiceID = @ChoiceID
    END


